<?php
class ModelArticleTemplate extends BaseModel {

    protected function table(){
        return "wp_posts";
    }

    protected function primary_key_name(){
        return "ID";
    }


    public function add($data) {
        $status = 'publish';

        $sql = "INSERT INTO " . $this->table() . " SET post_title = '{$data['post_title']}',post_content = '{$data['post_content']}',post_status='{$status}',post_type='template'";
        $this->db->query($sql);

        $id = $this->db->getLastId();


        return $id;
    }

    public function edit($id, $data) {
        $status = 'publish';



        $sql = "UPDATE " . $this->table() . " SET post_title = '{$data['post_title']}',post_content = '{$data['post_content']}',post_status='{$status}' WHERE {$this->primary_key_name()}={$id}";
        $this->db->query($sql);



        return $id;
    }

    public function delete($id) {
        parent::delete($id);
        $this->db->query("DELETE FROM wp_term_relationships WHERE object_id={$id}");
        $this->db->query("DELETE FROM customer_article WHERE post_id={$id}");
        $this->db->query("DELETE FROM customer_message WHERE post_id={$id}");
    }

    public function getCount() {
        $sql = "SELECT a.*  FROM " . DB_PREFIX . $this->table() . " a  WHERE a.post_type = 'template' ";
        if (!empty($data['filter_title'])) {
            $sql .= " AND post_title LIKE '{$this->db->escape($data['filter_title'])}%'";
        }
        if (!empty($data['filter_category'])) {
            $sql .= " AND exists(SELECT 1 from  wp_term_relationships b
                    join wp_term_taxonomy c on b.term_taxonomy_id = c.term_taxonomy_id
                    join wp_terms d on c.term_id = d.term_id
                    where b.object_id = a.ID and  c.term_taxonomy_id = {$data['filter_category']}) ";
        }

        $query = $this->db->query("SELECT COUNT(*) AS total FROM (" .$sql . ") a" );

        return $query->row['total'];
    }

    public function getList($data = array()) {
        $sql = "SELECT a.*  FROM " . DB_PREFIX . $this->table() . " a  WHERE a.post_type = 'template' ";
        if (!empty($data['filter_title'])) {
            $sql .= " AND post_title LIKE '{$this->db->escape($data['filter_title'])}%'";
        }
        if (!empty($data['filter_category'])) {
            $sql .= " AND exists(SELECT 1 from  wp_term_relationships b
                    join wp_term_taxonomy c on b.term_taxonomy_id = c.term_taxonomy_id
                    join wp_terms d on c.term_id = d.term_id
                    where b.object_id = a.ID and  c.term_taxonomy_id = {$data['filter_category']}) ";
        }

        $sort_data = array(
            $this->primary_key_name()
        );

        if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
            $sql .= " ORDER BY " . $data['sort'];
        } else {
            $sql .= " ORDER BY " . $sort_data[0];
        }

        if (isset($data['order']) && ($data['order'] == 'ASC')) {
            $sql .= " ASC";
        } else {
            $sql .= " DESC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }

        $query = $this->db->query($sql);

        $ret = $query->rows;


        return $ret;
    }









}